/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.services.manager;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.NullInputStream;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Required;

import java.io.*;

/**
 * Manager for file system based operations.
 * 
 * @author John Ericksen
 */
public class FileSystemManagerImpl implements FileSystemManager{

    private static final Logger LOG = Logger.getLogger(FileSystemManagerImpl.class);

    private String fileResourcesBase;

    /**
     * Get the input stream associated with the relative path.
     *
     * @param resourceIdentifier
     * @return InputStream for resource
     */
    public InputStream getStream(String directory, String resourceIdentifier) {
        InputStream inputStream = new NullInputStream(0);

        try {
            File file = getRelativeFile(directory, resourceIdentifier, false);
            if(file != null){
                inputStream = new FileInputStream(file);
            }
        } catch (Exception e) {
            LOG.error(e);
            inputStream = new NullInputStream(0);
        }

        return inputStream;
    }
    
    /**
     * Puts the given input into the resource identified with the given relative path.
     *
     * @param input
     * @param resourceIdentifier
     */
    public void putStreamToResource(InputStream input, String resourceDirectory, String resourceIdentifier) {
        try {
            File file = getRelativeFile(resourceDirectory, resourceIdentifier, true);
            if(file != null){
                OutputStream out = new FileOutputStream(file);
                IOUtils.copy(input, out);
            }
        } catch (FileNotFoundException e) {
            LOG.error(e);
        } catch (IOException e) {
            LOG.error(e);
        }
    }

    private File getRelativeFile(String resourceDirectory, String fileName, boolean create) throws IOException {
        File dir = new File(fileResourcesBase + File.separator + resourceDirectory);

        if(create && !dir.exists() && !dir.mkdirs()){
            return null;
        }

        File relativeFile = new File(dir, fileName);

        if(create && !relativeFile.exists() && !relativeFile.createNewFile()){
            return null;
        }
        
        return relativeFile;
    }

    @Required
    public void setFileResourcesBase(String fileResourcesBase) {
        this.fileResourcesBase = fileResourcesBase;
    }
}
